package com.gsn.chooseRoom;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.scenes.scene2d.ui.FlickScrollPane;
import com.gsn.common.PortalChangeGoldLayer;
import com.gsn.common.PortalCommingLayer;
import com.gsn.common.PortalDialogLayer;
import com.gsn.common.PortalDialogYesNoLayer.EButtonType;
import com.gsn.common.PortalDialogYesNoLayer.EDialogType;
import com.gsn.common.PortalDialogYesNoLayer.IDialogYesNoListener;
import com.gsn.common.PortalHelpLayer;
import com.gsn.common.PortalNotEnoughMoneyLayer;
import com.gsn.common.ShopGoldLayer;
import com.gsn.dataCommon.ConfigModel;
import com.gsn.dataCommon.UserData;
import com.gsn.engine.myplay.GsnGame;
import com.gsn.engine.myplay.GsnScreen;
import com.gsn.name.StatePortal;
import com.gsn.poker.asset.PortalTexture;
import com.gsn.poker.game.MyPoker;
import com.gsn.poker.packet.PacketFactory;

public class ChooseRoomScreen extends GsnScreen implements IDialogYesNoListener {
	public ChooseRoomLayer chooseRoomLayer;
	public PortalDialogLayer dialogLayer;
	public PortalCommingLayer commingLayer;
	public PortalHelpLayer helpLayer;
	public PortalChangeGoldLayer changeGoldLayer;
	public PortalNotEnoughMoneyLayer notEnoughLayer;
	public MakeRoomLayer makeRoomLayer;
	public ShopGoldLayer shopGoldLayer;

	public static final String NOT_ENOUGH_MONEY = "notEnoughMoney";
	public static final String CHANGE_GOLD_SUCCESS = "changeGoldSuccess";
	public static final String SHOW_DIALOG = "showDialog";
	public static final String DISCONNECT_DLG = "disconnect";

	public ChooseRoomScreen(float width, float height) {
		super(width, height);
	}

	@Override
	public void setDefaultInputListener() {
		chooseRoomLayer.setInputListener();
	}

	@Override
	public void onInit() {
		// TODO Auto-generated method stub
		chooseRoomLayer = new ChooseRoomLayer(width, height);
		addLayer(chooseRoomLayer);

		commingLayer = new PortalCommingLayer(width, height);
		addLayer(commingLayer, 1, false);

		helpLayer = new PortalHelpLayer(width, height);
		addLayer(helpLayer, 2, false);

		shopGoldLayer = new ShopGoldLayer(width, height);
		addLayer(shopGoldLayer, 3, false);

		changeGoldLayer = new PortalChangeGoldLayer(width, height);
		addLayer(changeGoldLayer, 4, false);

		makeRoomLayer = new MakeRoomLayer(width, height);
		addLayer(makeRoomLayer, 4, false);

		notEnoughLayer = new PortalNotEnoughMoneyLayer(width, height);
		addLayer(notEnoughLayer, 4, false);

		dialogLayer = new PortalDialogLayer();
		dialogLayer.setDialogListener(ChooseRoomScreen.this);

		addLayer(dialogLayer, 5, false);

		setDefaultInputListener();

	}

	@Override
	protected void setAssetNeed() {
		// TODO Auto-generated method stub
		addAsset(PortalTexture.inst());
	}

	@Override
	public void clickButton(String nameDlg, EButtonType btn) {
		// TODO Auto-generated method stub
		switch (btn) {
		case YES:

			if (nameDlg.equals(DISCONNECT_DLG)) {
				MyPoker.game.setChooseGameScreen();
				// quay tro lai man hinh chon game
			}

			break;

		default:
			break;
		}
	}

	public void showCommingDialog() {
		// TODO Auto-generated method stub
		commingLayer.show();
	}

	public void showHelpDialog() {
		// TODO Auto-generated method stub
		helpLayer.show();
	}

	public void updateEnterGame() {
		// TODO Auto-generated method stub
		if (chooseRoomLayer != null) {
			chooseRoomLayer.updateEnterGame();
			chooseRoomLayer.scrollChooseRoom.setWidget(chooseRoomLayer.listChooseRoom);
		}
	}

	public void updateRoom() {
		if (chooseRoomLayer != null) {
			chooseRoomLayer.updateListRoom();
			chooseRoomLayer.scrollChooseRoom.setWidget(chooseRoomLayer.listChooseRoom);

		}

	}

	public void updateMoney() {
		// TODO Auto-generated method stub
		if (chooseRoomLayer != null) {
			chooseRoomLayer.updateInfoUser();

			if (MyPoker.game.statePortal.equals(StatePortal.CHOOSE_ROOM)) {
				if (UserData.gold < ConfigModel.beanNeedSupport && UserData.numSupport > 0 && UserData.giftIndex <= 0) {

					MyPoker.client.send(PacketFactory.getSupportBean());
				} else if (UserData.gold < ConfigModel.channelModelList[ConfigModel.selectedChannel].goldStart) {
					MyPoker.client.send(PacketFactory.createExitChannel());
					MyPoker.game.setChooseGameScreen();
				}
			}
		}
	}

	public void showNotEnoughMoney() {
		notEnoughLayer.show();

	}

	public void showChangeGold(long xu, long gold, int id) {
		changeGoldLayer.show(xu, gold, id);
	}

	public void showChangeGoldSuccess() {
		// TODO Auto-generated method stub
		dialogLayer.createDialog(EDialogType.YES, CHANGE_GOLD_SUCCESS, "Bạn đổi G thành công");
		dialogLayer.show();
	}

	public void showShopGold() {
		shopGoldLayer.show();
	}

	public void updateRoomNum() {
		// TODO Auto-generated method stub
		chooseRoomLayer.listChooseRoom.updateRoomNum();
	}

	public void showMakeRoom() {
		makeRoomLayer.show();
	}

	@Override
	public GsnGame getGame() {
		// TODO Auto-generated method stub
		return MyPoker.game;
	}

	public void showDialog(String string) {
		// TODO Auto-generated method stub
		dialogLayer.createDialog(EDialogType.YES, SHOW_DIALOG, string);
		dialogLayer.show();
	}

	@Override
	public void onShowScreen() {
		// TODO Auto-generated method stub
		super.onShowScreen();
		if (chooseRoomLayer != null) {
			if (UserData.gold < ConfigModel.beanNeedSupport && UserData.numSupport > 0 && UserData.giftIndex <= 0) {

				MyPoker.client.send(PacketFactory.getSupportBean());
			} else if (UserData.gold < ConfigModel.channelModelList[ConfigModel.selectedChannel].goldStart) {
				MyPoker.client.send(PacketFactory.createExitChannel());
				MyPoker.game.setChooseGameScreen();
			}
		}
	}

	public void showDisconnectDlg() {
		if (dialogLayer != null) {
			dialogLayer.createDialog(EDialogType.YES, DISCONNECT_DLG, "Bạn bị mất kết nối!!!");
			dialogLayer.show();
		} else {
			MyPoker.game.setChooseGameScreen();
		}
	}

	public void hideAllLayer() {
		// TODO Auto-generated method stub
		if (dialogLayer != null)
			dialogLayer.hide();

		if (commingLayer != null)
			commingLayer.hide();
		if (helpLayer != null)
			helpLayer.hide();
		if (changeGoldLayer != null) {
			changeGoldLayer.hide();
		}
		if (notEnoughLayer != null) {
			notEnoughLayer.hide();
		}
		if (makeRoomLayer != null) {
			makeRoomLayer.hide();
		}
		if (shopGoldLayer != null) {
			shopGoldLayer.hide();
		}
		Gdx.input.setInputProcessor(chooseRoomLayer);
	}

}
